
#include "GraphicsObject.h"
#include "Resizer.h"
#include "Tag.h"

//-------------------------------------

GraphicsObject::GraphicsObject( QGraphicsObject * Owner ) :
    scene( 0 ),
    canResize( true ),
    _show( true )
{
    tagList.clear();

    for( int i=0; i < dotCount; ++i )
    {
        Resizer* r = new Resizer( this, i );
        r->setParentItem( this );
        r->hide();
        resizingPoints.append( r );
    }
    //setCacheMode( QGraphicsItem::DeviceCoordinateCache );
}

//-------------------------------------

void GraphicsObject::RmTag( Tag * t )
{
    if( tagList.contains( t ) )
    {
        if( t->HasChildren() )
        {
            QList< Tag* > * tl = t->GetTagList();
            QList< Tag* >::iterator i = tl->begin();
            while( i != tl->end() )
            {
                t->RmTag( *i );
            }
        }
        tagList.removeAll( t );
        delete t;
    }
}

//-------------------------------------

void GraphicsObject::RmAllTags()
{
    QList< Tag* >::iterator i = tagList.begin();
    while( i != tagList.end() )
    {
        RmTag( *i );
    }
}

//-------------------------------------

void GraphicsObject::ResizingPoints( const RPType t )
{
    if( resizingPoints.count() )
    {
        for( int i=0; i < dotCount; i++ )
        {
            switch( t )
            {
                case rpShow:
                    resizingPoints[i]->show();
                break;

                case rpHide:
                    resizingPoints[i]->hide();
                break;

                case rpUpdate:
                    if( !resizingPoints[i]->IsDragging )
                        resizingPoints[i]->UpdatePosition();
                break;

                case rpNone:
                break;
            }
        }
    }
}

//-------------------------------------

void GraphicsObject::Resize( QRectF newSize )
{
    if( ! canResize )
        return;
    rect = newSize;
    ResizingPoints( rpUpdate );
    prepareGeometryChange();
}

//-------------------------------------


